package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddWechat;
import com.ruoyi.system.domain.Favorites;
import com.ruoyi.system.enumutil.AddWechatEnum;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.AddWechatService;
import com.ruoyi.system.service.FavoritesService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/*
 *  @author 雨安
 *  类名： AddWechatController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/AddWechat")
public class AddWechatController {

    @Resource
    private AddWechatService addWechatService;

    //用户表
    @Resource
    private SysUserMapper sysUserMapper;

    //收藏表
    @Resource
    private FavoritesService favoritesService;

    //查询
    @PostMapping("/get/AddWechat")
    public AjaxResult getAddWechat(@RequestBody AddWechat addWechat){
        //创建分页对象
        Page<AddWechat> page = new Page<>();
        //设置页码
        page.setCurrent(addWechat.getCurrent());
        //设置行数
        page.setSize(addWechat.getSize());

        //执行查询
        addWechatService.page(page,new LambdaQueryWrapper<AddWechat>()
                //微信昵称
                .eq(Objects.nonNull(addWechat.getUserId()),AddWechat::getUserId,addWechat.getUserId())
                //审核状态
                .eq(Objects.nonNull(addWechat.getStatus()),AddWechat::getStatus,addWechat.getStatus())
                //举报状态
                .eq(Objects.nonNull(addWechat.getReportStatus()),AddWechat::getReportStatus,addWechat.getReportStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(AddWechat::getCreateTime)
                //添加分页语句
                .last(" limit " + (addWechat.getCurrent() - 1) * addWechat.getSize() +"," + addWechat.getSize())
        );
        page.setTotal(addWechatService.count(new LambdaQueryWrapper<AddWechat>()
                //微信昵称
                .eq(Objects.nonNull(addWechat.getUserId()),AddWechat::getUserId,addWechat.getUserId())
                //审核状态
                .eq(Objects.nonNull(addWechat.getStatus()),AddWechat::getStatus,addWechat.getStatus())
                //举报状态
                .eq(Objects.nonNull(addWechat.getReportStatus()),AddWechat::getReportStatus,addWechat.getReportStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(AddWechat::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/AddWechat/{id}")
    public AjaxResult deleteAddWechat(@PathVariable(value = "id")Long id){
        //调用Service层删除
        addWechatService.update(null,new UpdateWrapper<AddWechat>().lambda()
                //主键id
                .eq(AddWechat::getId,id)
                //设置逻辑删除为 真
                .set(AddWechat::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }

    //增加
    @PostMapping("/insert/AddWechat")
    public AjaxResult insertAddWechat(@RequestBody AddWechat addWechat){
        //设置默认值
        addWechat.setCreateBy(SecurityUtils.getUsername());
        addWechat.setCreateTime(new Date());
        addWechat.setUpdateBy(SecurityUtils.getUsername());
        addWechat.setUpdateTime(new Date());
        addWechat.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        addWechatService.save(addWechat);
        //返回结果
        return AjaxResult.success("新增成功");
    }


    //修改
    @PostMapping("/update/AddWechat")
    public AjaxResult updateAddWechat(@RequestBody AddWechat addWechat){
        System.out.println("进入了此处" + addWechat);
        //设置默认值
        addWechat.setUpdateTime(new Date());
        addWechat.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        addWechatService.updateById(addWechat);
        //返回结果
        return AjaxResult.success("修改成功");
    }





    //修改举报addwechat
    @PostMapping("/update/AddWechatJB")
    public AjaxResult updateAddWechatReports(@RequestBody AddWechat addWechat){
        System.out.println("进入了此处" + addWechat);
        //设置默认值
        addWechat.setUpdateTime(new Date());
        addWechat.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        addWechatService.updateById(addWechat);
        System.out.println(333333333);
        //返回结果
        return AjaxResult.success("修改成功");
    }


//    //修改个人名片
//    @PostMapping("/get/updateMybusinesscard")
//    public AjaxResult updateMybusinesscard(@RequestBody AddWechat addWechat){
//        //获取原来的add对象
//        AddWechat yAdd = addWechatService.getOne(new LambdaQueryWrapper<AddWechat>()
//                .eq(AddWechat::getId, addWechat.getId())
//        );
//        //判断人数是否不相等
//        if (yAdd.getNeedCount() > addWechat.getNeedCount()){
//            System.out.println("进入此处需要给用户反钱");
//            //获得需要退款人数的数量
//            Integer resultCount = yAdd.getNeedCount() - addWechat.getNeedCount();
//            //获取退款金额
//            Double  resultFund = resultCount * 1.5;
//            //修改个人余额积分
//            SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
//            //设置用户余额积分
//            sysUser.setScore(sysUser.getScore().add(BigDecimal.valueOf(resultFund)));
//            //设置默认值
//            sysUser.setUpdateTime(new Date());
//            sysUser.setUpdateBy(SecurityUtils.getUsername());
//            //执行修改
//            sysUserMapper.updateUser(sysUser);
//            //创建订单对象
//            OrderInfo orderInfo = new OrderInfo();
//            //设置订单号
//            orderInfo.setOrderNo("用户修改个人名片,人数变少退回部分积分,无订单号");
//            //设置金额
//            orderInfo.setFund(BigDecimal.valueOf(resultFund));
//            //设置用户编号
//            orderInfo.setUserId(SecurityUtils.getUserId());
//            //设置订单备注
//            orderInfo.setDescription("好友任务需求人数减少,退回部分积分。");
//            //设置订单状态
//            orderInfo.setOrderType(OrderInfoEnum.MingPianTuiKuan.getType());
//            //设置默认值
//            orderInfo.setCreateTime(new Date());
//            orderInfo.setCreateBy(SecurityUtils.getUsername());
//            orderInfo.setUpdateTime(new Date());
//            orderInfo.setUpdateBy(SecurityUtils.getUsername());
//            orderInfo.setDeleteFlag(Boolean.FALSE);
//            //执行添加
//            orderInfoMapper.insert(orderInfo);
//        }
//        if (yAdd.getNeedCount() < addWechat.getNeedCount()){
//            System.out.println("进入此处需要给用户扣钱");
//            //获取需要增加的人数
//            Integer resultCount = addWechat.getNeedCount() - yAdd.getNeedCount();
//            //获取退款金额
//            Double  resultFund = resultCount * 1.5;
//            //查询个人余额积分
//            SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
//            //判断是否有足够多的积分
//            if (sysUser.getScore().compareTo(BigDecimal.valueOf(resultFund)) >= 0) {
//                //设置用户余额积分
//                sysUser.setScore(sysUser.getScore().subtract(BigDecimal.valueOf(resultFund)));
//                //设置默认值
//                sysUser.setUpdateTime(new Date());
//                sysUser.setUpdateBy(SecurityUtils.getUsername());
//                //执行修改
//                sysUserMapper.updateUser(sysUser);
//                //创建订单对象
//                OrderInfo orderInfo = new OrderInfo();
//                //设置订单号
//                orderInfo.setOrderNo("用户修改个人名片,人数增加扣款金额,无订单号");
//                //设置金额
//                orderInfo.setFund(BigDecimal.valueOf(resultFund));
//                //设置用户编号
//                orderInfo.setUserId(SecurityUtils.getUserId());
//                //设置订单备注
//                orderInfo.setDescription("好友任务需求人数增加,扣除部分积分。");
//                //设置订单状态
//                orderInfo.setOrderType(OrderInfoEnum.MingPianKouKuan.getType());
//                //设置默认值
//                orderInfo.setCreateTime(new Date());
//                orderInfo.setCreateBy(SecurityUtils.getUsername());
//                orderInfo.setUpdateTime(new Date());
//                orderInfo.setUpdateBy(SecurityUtils.getUsername());
//                orderInfo.setDeleteFlag(Boolean.FALSE);
//                //执行添加
//                orderInfoMapper.insert(orderInfo);
//            }
//        }else{
//            return AjaxResult.error("您的积分不足,无法将修改需求人数进行添加。");
//        }
//        //设置默认值
//        addWechat.setUpdateTime(new Date());
//        addWechat.setDeleteFlag(Boolean.FALSE);
//        //调用Service层删除
//        addWechatService.updateById(addWechat);
//        //返回结果
//        return AjaxResult.success("修改成功");
//    }



    //获取个人名片,拿到id
    @PostMapping("/get/mybusinesscard")
    public AjaxResult selectGetWechat(@RequestBody AddWechat addWechat){
        //获取用户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());

        System.out.println(addWechat + "我是测试sss");
        System.out.println(sysUser);
        addWechat.setUserId(sysUser.getUserId());
        //创建分页对象
        Page<AddWechat> page = new Page<>();
        //设置页码
        page.setCurrent(addWechat.getCurrent());
        //设置行数
        page.setSize(addWechat.getSize());
        //查询数据
        addWechatService.page(page,new LambdaQueryWrapper<AddWechat>()
                //根据UserId查询
                .eq(Objects.nonNull(addWechat.getUserId()),AddWechat::getUserId,addWechat.getUserId())
                //审核通过
                .eq(AddWechat::getStatus, AddWechatEnum.ShenHeTongGuo.getStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //创建时间
                .orderByDesc(AddWechat::getCreateTime)
        );
        //设置总行数
        page.setTotal(addWechatService.count(new LambdaQueryWrapper<AddWechat>()
                //根据UserId查询
                .eq(Objects.nonNull(addWechat.getUserId()),AddWechat::getUserId,addWechat.getUserId())
                //审核通过
                .eq(AddWechat::getStatus, AddWechatEnum.ShenHeTongGuo.getStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //创建时间
                .orderByDesc(AddWechat::getCreateTime)
        ));

        System.out.println(AjaxResult.success(page));
        System.out.println(121212);
        //返回结果
        return AjaxResult.success(page);
    }


    //获取个人收藏
    @PostMapping("/get/favorites")
    public AjaxResult selectGetFavorites(@RequestBody AddWechat addWechat){
        //获取用户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());

        Favorites favorites = new Favorites();
        favorites.setUserId(sysUser.getUserId());

        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",sysUser.getUserId());

        List<Favorites> list = favoritesService.list(queryWrapper);
        List<Long> list1 = list.stream().map(Favorites::getAddId).collect(Collectors.toList());
        //判断list1为空，当用户没有收藏任务
        if(list1 != null && list1.size() == 0){
            Page<AddWechat> addWechatPage = new Page<>();
            addWechatPage.setTotal(0);
            addWechatPage.setRecords(new ArrayList<>());
            return AjaxResult.success(addWechatPage);
        }
        System.out.println(111111111);
        System.out.println(list1);
        //创建分页对象
        Page<AddWechat> page = new Page<>();
        //设置页码
        page.setCurrent(addWechat.getCurrent());
        //设置行数
        page.setSize(addWechat.getSize());
        //查询数据
        addWechatService.page(page,new LambdaQueryWrapper<AddWechat>()
                //根据UserId查询
                .in(AddWechat::getId,list1)
                //审核通过
                .eq(AddWechat::getStatus, AddWechatEnum.ShenHeTongGuo.getStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //创建时间
                .orderByDesc(AddWechat::getCreateTime)
        );
        //设置总行数
        page.setTotal(addWechatService.count(new LambdaQueryWrapper<AddWechat>()
                //根据UserId查询
                .in(AddWechat::getId,list1)
                //审核通过
                .eq(AddWechat::getStatus, AddWechatEnum.ShenHeTongGuo.getStatus())
                //逻辑删除
                .eq(AddWechat::getDeleteFlag,Boolean.FALSE)
                //创建时间
                .orderByDesc(AddWechat::getCreateTime)
        ));

        System.out.println(AjaxResult.success(page));
        System.out.println(121212);
        System.out.println(page);
        //返回结果
        return AjaxResult.success(page);
    }

//    //下架名片
//    @PostMapping("/delete/AddWechat/XJ")
//    public AjaxResult deleteAddWechat(@RequestBody AddWechat addWechat){
//        //调用Service层删除
//        addWechatService.update(null,new UpdateWrapper<AddWechat>().lambda()
//                //主键id
//                .eq(AddWechat::getId,addWechat.getId())
//                //设置逻辑删除为 真
//                .set(AddWechat::getDeleteFlag,Boolean.TRUE)
//        );
//        //返回结果
//        return AjaxResult.success("删除成功");
//    }

}
